home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 322 / flex / flexdef.h < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  18.1 KB  |  512 lines

  1. /*
  2.  *  Definitions for flex.
  3.  *
  4.  * modification history
  5.  * --------------------
  6.  * 02b kg, vp   30sep87  .added definitions for fast scanner; misc. cleanup
  7.  * 02a vp       27jun86  .translated into C/FTL
  8.  */
  9.  
  10. /*
  11.  * Copyright (c) 1987, the University of California
  12.  * 
  13.  * The United States Government has rights in this work pursuant to
  14.  * contract no. DE-AC03-76SF00098 between the United States Department of
  15.  * Energy and the University of California.
  16.  * 
  17.  * This program may be redistributed.  Enhancements and derivative works
  18.  * may be created provided the new works, if made available to the general
  19.  * public, are made available for use by anyone.
  20.  */
  21.  
  22. #include <stdio.h>
  23.  
  24. #ifdef atarist
  25. #define DEFAULT_SKELETON_FILE def_skel_file()
  26. #define FAST_SKELETON_FILE fast_skel_file()
  27. #define BUFSIZ    512        /* ??? */
  28. #endif
  29.  
  30. #ifdef SV
  31. #include <string.h>
  32. #define bzero(s, n) memset((char *)(s), '\000', (unsigned)(n))
  33. #else
  34. #include <strings.h>
  35. #endif
  36.  
  37. char *sprintf(); /* keep lint happy */
  38.  
  39.  
  40. /* maximum line length we'll have to deal with */
  41. #define MAXLINE BUFSIZ
  42.  
  43. /* maximum size of file name */
  44. #define FILENAMESIZE 1024
  45.  
  46. #define min(x,y) (x < y ? x : y)
  47. #define max(x,y) (x > y ? x : y)
  48.  
  49. #define true 1
  50. #define false 0
  51.  
  52.  
  53. #ifndef DEFAULT_SKELETON_FILE
  54. #define DEFAULT_SKELETON_FILE "flex.skel"
  55. #endif
  56.  
  57. #ifndef FAST_SKELETON_FILE
  58. #define FAST_SKELETON_FILE "flex.fastskel"
  59. #endif
  60.  
  61. /* special nxt[] action number for the "at the end of the input buffer" state */
  62. /* note: -1 is already taken by YY_NEW_FILE */
  63. #define END_OF_BUFFER_ACTION -3
  64. /* action number for default action for fast scanners */
  65. #define DEFAULT_ACTION -2
  66.  
  67. /* special chk[] values marking the slots taking by end-of-buffer and action
  68.  * numbers
  69.  */
  70. #define EOB_POSITION -1
  71. #define ACTION_POSITION -2
  72.  
  73. /* number of data items per line for -f output */
  74. #define NUMDATAITEMS 10
  75.  
  76. /* number of lines of data in -f output before inserting a blank line for
  77.  * readability.
  78.  */
  79. #define NUMDATALINES 10
  80.  
  81. /* transition_struct_out() definitions */
  82. #define TRANS_STRUCT_PRINT_LENGTH 15
  83.  
  84. /* returns true if an nfa state has an epsilon out-transition slot
  85.  * that can be used.  This definition is currently not used.
  86.  */
  87. #define FREE_EPSILON(state) \
  88.     (transchar[state] == SYM_EPSILON && \
  89.      trans2[state] == NO_TRANSITION && \
  90.      finalst[state] != state)
  91.  
  92. /* returns true if an nfa state has an epsilon out-transition character
  93.  * and both slots are free
  94.  */
  95. #define SUPER_FREE_EPSILON(state) \
  96.     (transchar[state] == SYM_EPSILON && \
  97.      trans1[state] == NO_TRANSITION) \
  98.  
  99. /* maximum number of NFA states that can comprise a DFA state.  It's real
  100.  * big because if there's a lot of rules, the initial state will have a
  101.  * huge epsilon closure.
  102.  */
  103. #define INITIAL_MAX_DFA_SIZE 750
  104. #define MAX_DFA_SIZE_INCREMENT 750
  105.  
  106. /* array names to be used in generated machine.  They're short because
  107.  * we write out one data statement (which names the array) for each element
  108.  * in the array.
  109.  */
  110.  
  111. #ifdef atarist
  112. #define ALIST "__l"    /* points to list of rules accepted for a state */
  113. #define ACCEPT "__a"    /* list of rules accepted for a state */
  114. #define ECARRAY "__e"    /* maps input characters to equivalence classes */
  115. #define MATCHARRAY "__m"    /* maps equivalence classes to meta-equivalence classes */
  116. #define BASEARRAY "__b"    /* "base" array */
  117. #define DEFARRAY "__d"    /* "default" array */
  118. #define NEXTARRAY "__n"    /* "next" array */
  119. #define CHECKARRAY "__c"    /* "check" array */
  120. #else
  121. #define ALIST 'l'    /* points to list of rules accepted for a state */
  122. #define ACCEPT 'a'    /* list of rules accepted for a state */
  123. #define ECARRAY 'e'    /* maps input characters to equivalence classes */
  124. #define MATCHARRAY 'm'    /* maps equivalence classes to meta-equivalence classes */
  125. #define BASEARRAY 'b'    /* "base" array */
  126. #define DEFARRAY 'd'    /* "default" array */
  127. #define NEXTARRAY 'n'    /* "next" array */
  128. #define CHECKARRAY 'c'    /* "check" array */
  129. #endif
  130.  
  131. /* NIL must be 0.  If not, its special meaning when making equivalence classes
  132.  * (it marks the representative of a given e.c.) will be unidentifiable
  133.  */
  134. #define NIL 0
  135.  
  136. #define JAM -1    /* to mark a missing DFA transition */
  137. #define NO_TRANSITION NIL
  138. #define UNIQUE -1    /* marks a symbol as an e.c. representative */
  139. #define INFINITY -1    /* for x{5,} constructions */
  140.  
  141. /* size of input alphabet - should be size of ASCII set */
  142. #define CSIZE 127
  143.  
  144. #define INITIAL_MAXCCLS 100    /* max number of unique character classes */
  145. #define MAXCCLS_INCREMENT 100
  146.  
  147. /* size of table holding members of character classes */
  148. #define INITIAL_MAX_CCL_TBL_SIZE 500
  149. #define MAX_CCL_TBL_SIZE_INCREMENT 250
  150.  
  151. #define INITIAL_MNS 2000    /* default maximum number of nfa states */
  152. #define MNS_INCREMENT 1000    /* amount to bump above by if it's not enough */
  153.  
  154. #define INITIAL_MAX_DFAS 1000    /* default maximum number of dfa states */
  155. #define MAX_DFAS_INCREMENT 1000
  156.  
  157. #define JAMSTATE -32766    /* marks a reference to the state that always jams */
  158.  
  159. /* enough so that if it's subtracted from an NFA state number, the result
  160.  * is guaranteed to be negative
  161.  */
  162. #define MARKER_DIFFERENCE 32000
  163. #define MAXIMUM_MNS 31999
  164.  
  165. /* maximum number of nxt/chk pairs for non-templates */
  166. #define INITIAL_MAX_XPAIRS 2000
  167. #define MAX_XPAIRS_INCREMENT 2000
  168.  
  169. /* maximum number of nxt/chk pairs needed for templates */
  170. #define INITIAL_MAX_TEMPLATE_XPAIRS 2500
  171. #define MAX_TEMPLATE_XPAIRS_INCREMENT 2500
  172.  
  173. #define SYM_EPSILON 0    /* to mark transitions on the symbol epsilon */
  174.  
  175. #define INITIAL_MAX_SCS 40    /* maximum number of start conditions */
  176. #define MAX_SCS_INCREMENT 40    /* amount to bump by if it's not enough */
  177.  
  178. #define ONE_STACK_SIZE 500    /* stack of states with only one out-transition */
  179. #define SAME_TRANS -1    /* transition is the same as "default" entry for state */
  180.  
  181. /* the following percentages are used to tune table compression:
  182.  
  183.  * the percentage the number of out-transitions a state must be of the
  184.  * number of equivalence classes in order to be considered for table
  185.  * compaction by using protos
  186.  */
  187. #define PROTO_SIZE_PERCENTAGE 15
  188.  
  189. /* the percentage the number of homogeneous out-transitions of a state
  190.  * must be of the number of total out-transitions of the state in order
  191.  * that the state's transition table is first compared with a potential 
  192.  * template of the most common out-transition instead of with the first
  193.  * proto in the proto queue
  194.  */
  195. #define CHECK_COM_PERCENTAGE 50
  196.  
  197. /* the percentage the number of differences between a state's transition
  198.  * table and the proto it was first compared with must be of the total
  199.  * number of out-transitions of the state in order to keep the first
  200.  * proto as a good match and not search any further
  201.  */
  202. #define FIRST_MATCH_DIFF_PERCENTAGE 10
  203.  
  204. /* the percentage the number of differences between a state's transition
  205.  * table and the most similar proto must be of the state's total number
  206.  * of out-transitions to use the proto as an acceptable close match
  207.  */
  208. #define ACCEPTABLE_DIFF_PERCENTAGE 50
  209.  
  210. /* the percentage the number of homogeneous out-transitions of a state
  211.  * must be of the number of total out-transitions of the state in order
  212.  * to consider making a template from the state
  213.  */
  214. #define TEMPLATE_SAME_PERCENTAGE 60
  215.  
  216. /* the percentage the number of differences between a state's transition
  217.  * table and the most similar proto must be of the state's total number
  218.  * of out-transitions to create a new proto from the state
  219.  */
  220. #define NEW_PROTO_DIFF_PERCENTAGE 20
  221.  
  222. /* the percentage the total number of out-transitions of a state must be
  223.  * of the number of equivalence classes in order to consider trying to
  224.  * fit the transition table into "holes" inside the nxt/chk table.
  225.  */
  226. #define INTERIOR_FIT_PERCENTAGE 15
  227.  
  228. /* size of region set aside to cache the complete transition table of
  229.  * protos on the proto queue to enable quick comparisons
  230.  */
  231. #define PROT_SAVE_SIZE 2000
  232.  
  233. #define MSP 50    /* maximum number of saved protos (protos on the proto queue) */
  234.  
  235. /* maximum number of out-transitions a state can have that we'll rummage
  236.  * around through the interior of the internal fast table looking for a
  237.  * spot for it
  238.  */
  239. #define MAX_XTIONS_FOR_FULL_INTERIOR_FIT 4
  240.  
  241. /* number that, if used to subscript an array, has a good chance of producing
  242.  * an error; should be small enough to fit into a short
  243.  */
  244. #define BAD_SUBSCRIPT -32767
  245.  
  246. /* absolute value of largest number that can be stored in a short, with a
  247.  * bit of slop thrown in for general paranoia.
  248.  */
  249. #define MAX_SHORT 32766
  250.  
  251.  
  252. /* Declarations for global variables. */
  253.  
  254. /* variables for symbol tables:
  255.  * sctbl - start-condition symbol table
  256.  * ndtbl - name-definition symbol table
  257.  * ccltab - character class text symbol table
  258.  */
  259.  
  260. struct hash_entry
  261.     {
  262.     struct hash_entry *prev, *next;
  263.     char *name;
  264.     char *str_val;
  265.     int int_val;
  266.     } ;
  267.  
  268. typedef struct hash_entry *hash_table[];
  269.  
  270. #define NAME_TABLE_HASH_SIZE 101
  271. #define START_COND_HASH_SIZE 101
  272. #define CCL_HASH_SIZE 101
  273.  
  274. extern struct hash_entry *ndtbl[NAME_TABLE_HASH_SIZE]; 
  275. extern struct hash_entry *sctbl[START_COND_HASH_SIZE];
  276. extern struct hash_entry *ccltab[CCL_HASH_SIZE];
  277.  
  278.  
  279. /* variables for flags:
  280.  * printstats - if true (-v), dump statistics
  281.  * syntaxerror - true if a syntax error has been found
  282.  * eofseen - true if we've seen an eof in the input file
  283.  * ddebug - if true (-d), make a "debug" scanner
  284.  * trace - if true (-T), trace processing
  285.  * spprdflt - if true (-s), suppress the default rule
  286.  * interactive - if true (-I), generate an interactive scanner
  287.  * caseins - if true (-i), generate a case-insensitive scanner
  288.  * useecs - if true (-ce flag), use equivalence classes
  289.  * fulltbl - if true (-cf flag), don't compress the DFA state table
  290.  * usemecs - if true (-cm flag), use meta-equivalence classes
  291.  * reject - if true (-r flag), generate tables for REJECT macro
  292.  * fullspd - if true (-F flag), use Jacobson method of table representation
  293.  * gen_line_dirs - if true (i.e., no -L flag), generate #line directives
  294.  */
  295.  
  296. extern int printstats, syntaxerror, eofseen, ddebug, trace, spprdflt;
  297. extern int interactive, caseins, useecs, fulltbl, usemecs, reject;
  298. extern int fullspd, gen_line_dirs;
  299.  
  300.  
  301. /* variables used in the flex input routines:
  302.  * datapos - characters on current output line
  303.  * dataline - number of contiguous lines of data in current data
  304.  *    statement.  Used to generate readable -f output
  305.  * skelfile - fd of the skeleton file
  306.  * yyin - input file
  307.  * temp_action_file - temporary file to hold actions
  308.  * action_file_name - name of the temporary file
  309.  * infilename - name of input file
  310.  * linenum - current input line number
  311.  */
  312.  
  313. extern int datapos, dataline, linenum;
  314. extern FILE *skelfile, *yyin, *temp_action_file;
  315.  
  316. #ifdef atarist
  317. extern FILE * yyout;
  318. #endif
  319.  
  320. extern char *infilename;
  321. extern char *action_file_name;
  322.  
  323.  
  324. /* variables for stack of states having only one out-transition:
  325.  * onestate - state number
  326.  * onesym - transition symbol
  327.  * onenext - target state
  328.  * onedef - default base entry
  329.  * onesp - stack pointer
  330.  */
  331.  
  332. extern int onestate[ONE_STACK_SIZE], onesym[ONE_STACK_SIZE];
  333. extern int onenext[ONE_STACK_SIZE], onedef[ONE_STACK_SIZE], onesp;
  334.  
  335.  
  336. /* variables for nfa machine data:
  337.  * current_mns - current maximum on number of NFA states
  338.  * accnum - number of the last accepting state
  339.  * firstst - physically the first state of a fragment
  340.  * lastst - last physical state of fragment
  341.  * finalst - last logical state of fragment
  342.  * transchar - transition character
  343.  * trans1 - transition state
  344.  * trans2 - 2nd transition state for epsilons
  345.  * accptnum - accepting number
  346.  * lastnfa - last nfa state number created
  347.  */
  348.  
  349. extern int current_mns;
  350. extern int accnum, *firstst, *lastst, *finalst, *transchar;
  351. extern int *trans1, *trans2, *accptnum, lastnfa;
  352.  
  353.  
  354. /* variables for protos:
  355.  * numtemps - number of templates created
  356.  * numprots - number of protos created
  357.  * protprev - backlink to a more-recently used proto
  358.  * protnext - forward link to a less-recently used proto
  359.  * prottbl - base/def table entry for proto
  360.  * protcomst - common state of proto
  361.  * firstprot - number of the most recently used proto
  362.  * lastprot - number of the least recently used proto
  363.  * protsave contains the entire state array for protos
  364.  */
  365.  
  366. extern int numtemps, numprots, protprev[MSP], protnext[MSP], prottbl[MSP];
  367. extern int protcomst[MSP], firstprot, lastprot, protsave[PROT_SAVE_SIZE];
  368.  
  369.  
  370. /* variables for managing equivalence classes:
  371.  * numecs - number of equivalence classes
  372.  * nextecm - forward link of Equivalence Class members
  373.  * ecgroup - class number or backward link of EC members
  374.  * nummecs - number of meta-equivalence classes (used to compress
  375.  *   templates)
  376.  * tecfwd - forward link of meta-equivalence classes members
  377.  * tecbck - backward link of MEC's
  378.  */
  379.  
  380. extern int numecs, nextecm[CSIZE + 1], ecgroup[CSIZE + 1], nummecs;
  381. extern int tecfwd[CSIZE + 1], tecbck[CSIZE + 1];
  382.  
  383.  
  384. /* variables for start conditions:
  385.  * lastsc - last start condition created
  386.  * current_max_scs - current limit on number of start conditions
  387.  * scset - set of rules active in start condition
  388.  * scbol - set of rules active only at the beginning of line in a s.c.
  389.  * scxclu - true if start condition is exclusive
  390.  * actvsc - stack of active start conditions for the current rule
  391.  */
  392.  
  393. extern int lastsc, current_max_scs, *scset, *scbol, *scxclu, *actvsc;
  394.  
  395.  
  396. /* variables for dfa machine data:
  397.  * current_max_dfa_size - current maximum number of NFA states in DFA
  398.  * current_max_xpairs - current maximum number of non-template xtion pairs
  399.  * current_max_template_xpairs - current maximum number of template pairs
  400.  * current_max_dfas - current maximum number DFA states
  401.  * lastdfa - last dfa state number created
  402.  * nxt - state to enter upon reading character
  403.  * chk - check value to see if "nxt" applies
  404.  * tnxt - internal nxt table for templates
  405.  * base - offset into "nxt" for given state
  406.  * def - where to go if "chk" disallows "nxt" entry
  407.  * tblend - last "nxt/chk" table entry being used
  408.  * firstfree - first empty entry in "nxt/chk" table
  409.  * dss - nfa state set for each dfa
  410.  * dfasiz - size of nfa state set for each dfa
  411.  * dfaacc - accepting set for each dfa state (or accepting number, if
  412.  *    -r is not given)
  413.  * accsiz - size of accepting set for each dfa state
  414.  * dhash - dfa state hash value
  415.  * todo - queue of DFAs still to be processed
  416.  * todo_head - head of todo queue
  417.  * todo_next - next available entry on todo queue
  418.  * numas - number of DFA accepting states created; note that this
  419.  *    is not necessarily the same value as accnum, which is the analogous
  420.  *    value for the NFA
  421.  * numsnpairs - number of state/nextstate transition pairs
  422.  * jambase - position in base/def where the default jam table starts
  423.  * jamstate - state number corresponding to "jam" state
  424.  * end_of_buffer_state - end-of-buffer dfa state number
  425.  */
  426.  
  427. extern int current_max_dfa_size, current_max_xpairs;
  428. extern int current_max_template_xpairs, current_max_dfas;
  429. extern int lastdfa, lasttemp, *nxt, *chk, *tnxt;
  430. extern int *base, *def, tblend, firstfree, **dss, *dfasiz;
  431. extern union dfaacc_union
  432.     {
  433.     int *dfaacc_set;
  434.     int dfaacc_state;
  435.     } *dfaacc;
  436. extern int *accsiz, *dhash, *todo, todo_head, todo_next, numas;
  437. extern int numsnpairs, jambase, jamstate;
  438. extern int end_of_buffer_state;
  439.  
  440. /* variables for ccl information:
  441.  * lastccl - ccl index of the last created ccl
  442.  * current_maxccls - current limit on the maximum number of unique ccl's
  443.  * cclmap - maps a ccl index to its set pointer
  444.  * ccllen - gives the length of a ccl
  445.  * cclng - true for a given ccl if the ccl is negated
  446.  * cclreuse - counts how many times a ccl is re-used
  447.  * current_max_ccl_tbl_size - current limit on number of characters needed
  448.  *    to represent the unique ccl's
  449.  * ccltbl - holds the characters in each ccl - indexed by cclmap
  450.  */
  451.  
  452. extern int lastccl, current_maxccls, *cclmap, *ccllen, *cclng, cclreuse;
  453. extern int current_max_ccl_tbl_size;
  454. extern char *ccltbl;
  455.  
  456.  
  457. /* variables for miscellaneous information:
  458.  * starttime - real-time when we started
  459.  * endtime - real-time when we ended
  460.  * nmstr - last NAME scanned by the scanner
  461.  * sectnum - section number currently being parsed
  462.  * nummt - number of empty nxt/chk table entries
  463.  * hshcol - number of hash collisions detected by snstods
  464.  * dfaeql - number of times a newly created dfa was equal to an old one
  465.  * numeps - number of epsilon NFA states created
  466.  * eps2 - number of epsilon states which have 2 out-transitions
  467.  * num_reallocs - number of times it was necessary to realloc() a group
  468.  *          of arrays
  469.  * tmpuses - number of DFA states that chain to templates
  470.  * totnst - total number of NFA states used to make DFA states
  471.  * peakpairs - peak number of transition pairs we had to store internally
  472.  * numuniq - number of unique transitions
  473.  * numdup - number of duplicate transitions
  474.  * hshsave - number of hash collisions saved by checking number of states
  475.  */
  476.  
  477. extern char *starttime, *endtime, nmstr[MAXLINE];
  478. extern int sectnum, nummt, hshcol, dfaeql, numeps, eps2, num_reallocs;
  479. extern int tmpuses, totnst, peakpairs, numuniq, numdup, hshsave;
  480.  
  481. char *allocate_array(), *reallocate_array();
  482.  
  483. #define allocate_integer_array(size) \
  484.     (int *) allocate_array( size, sizeof( int ) )
  485.  
  486. #define reallocate_integer_array(array,size) \
  487.     (int *) reallocate_array( (char *) array, size, sizeof( int ) )
  488.  
  489. #define allocate_integer_pointer_array(size) \
  490.     (int **) allocate_array( size, sizeof( int * ) )
  491.  
  492. #define allocate_dfaacc_union(size) \
  493.     (union dfaacc_union *) \
  494.         allocate_array( size, sizeof( union dfaacc_union ) )
  495.  
  496. #define reallocate_integer_pointer_array(array,size) \
  497.     (int **) reallocate_array( (char *) array, size, sizeof( int * ) )
  498.  
  499. #define reallocate_dfaacc_union(array, size) \
  500.     (union dfaacc_union *)  reallocate_array( (char *) array, size, sizeof( union dfaacc_union ) )
  501.  
  502. #define allocate_character_array(size) allocate_array( size, sizeof( char ) )
  503.  
  504. #define reallocate_character_array(array,size) \
  505.     reallocate_array( array, size, sizeof( char ) )
  506.  
  507.  
  508. /* used to communicate between scanner and parser.  The type should really
  509.  * be YYSTYPE, but we can't easily get our hands on it.
  510.  */
  511. extern int yylval;
  512.